/**
 * Copyright(C) 2012 
 * 版权所有(C)2012 
 * <p>
 * Compiler: JDK1.6.0
 * <p>
 * 版本:  1.0版
 * 文件名：JSONUtils.java
 * <p>
 * 作者: hu_hengjiu
 * <p>
 * 创建时间: 2012-3-30
 * <p>
 * 负责人: hu_hengjiu
 * <p>
 * 修改者：hu_hengjiu（修改者姓名）,修改时间：2012-3-30
 * <p>
 */
package com.free.tools;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.free.commons.form.DynamicForm;
import com.free.constants.FrameworkConstant;

/**
 * JSON工具类
 *
 */
public class JSONUtils {
	private JSONUtils()
	{
	}
	
	@SuppressWarnings("unchecked")
	public static JSONObject parseToObject(Map<String, Object> sourceMap)
	{
		JSONObject result = new JSONObject();
		Object pageSize = sourceMap.get(FrameworkConstant.RESULT_COUNT);
		if(pageSize == null)
			return null;
		result.put(FrameworkConstant.DATAGRID_TOTAL, pageSize);
		JSONArray array = new JSONArray ();
		List<DynamicForm> list  = (List<DynamicForm>)sourceMap.get(FrameworkConstant.RESULT_DATA);
		for(DynamicForm dynamicForm : list)
		{
			JSONObject jsonObj = new JSONObject();
			for(Iterator<Object> itrator=dynamicForm.getMap().keySet().iterator(); itrator.hasNext();)
			{
				Object key = itrator.next();
				jsonObj.put(key, dynamicForm.get(key));
			}
			array.add(jsonObj);
		}
		result.put(FrameworkConstant.DATAGRID_ROWS, array);
		return result;
	}
}
